package com.yy.permissions_general.module.system.schedule.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.permissions_general.module.system.schedule.entity.ScheduleJob;
import com.yy.permissions_general.module.system.schedule.handler.ScheduleHandler;
import com.yy.permissions_general.module.system.schedule.mapper.ScheduleJobMapper;
import com.yy.permissions_general.module.system.schedule.service.ScheduleJobService;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 定时任务 服务实现类
 * </p>
 *
 * @author YY
 * @since 2022-02-09
 */
@Service
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobMapper, ScheduleJob> implements ScheduleJobService {
    /**
     * 引 入 服 务
     */
    @Resource
    private Scheduler scheduler;
    /**
    * 分页查询列表
    * @param scheduleJob
    * @return
    */
    @Override
    public  Page<ScheduleJob>selectPageList(ScheduleJob scheduleJob, int limit, int page)  {
        Page pageContext = new Page(page,limit);
        Page<ScheduleJob> pageList = this.baseMapper.selectPage(pageContext, scheduleJob);
        return pageList;
    }

    /**
    * 新增数据
    * @param scheduleJob
    */
    @Override
    public int addScheduleJob(ScheduleJob scheduleJob) {
        return baseMapper.insert(scheduleJob);
    }

    /**
    * 修改数据
    * @param scheduleJob
    */
    @Override
    public int editScheduleJob(ScheduleJob scheduleJob) {
        return baseMapper.updateById(scheduleJob);
    }

    /**
    * 查看详情
    * @param jobId
    * @return
    */
    @Override
    public ScheduleJob getDetailsById(String jobId) {
        return baseMapper.selectById(jobId);
    }

    /**
    * 删除数据
    * @param jobId
    * @return
    */
    @Override
    public int deleteById(String jobId) {
        return baseMapper.deleteById(jobId);
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @Override
    public int deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateScheduleJob(ScheduleJob scheduleJob) {
        ScheduleHandler.updateJob(scheduler, scheduleJob);
        baseMapper.updateById(scheduleJob);
    }

    @Override
    public void submitScheduleJob(String jobId) {
        ScheduleJob scheduleJob = baseMapper.selectById(jobId);
        ScheduleHandler.run(scheduler, scheduleJob);
    }

    @Override
    public void stopScheduleJob(String jobId) {
        ScheduleJob scheduleJob = baseMapper.selectById(jobId);
        ScheduleHandler.pauseJob(scheduler, Long.parseLong(jobId));
        scheduleJob.setStatus(1);
        baseMapper.updateById(scheduleJob);
    }

    @Override
    public void resetScheduleJob(String jobId) {
        ScheduleJob scheduleJob = baseMapper.selectById(jobId);
        ScheduleHandler.resumeJob(scheduler, Long.parseLong(jobId));
        scheduleJob.setStatus(0);
        baseMapper.updateById(scheduleJob);
    }

}

